package com.one.group.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.admin.mapper.SpChargeItemsMapper;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.SpChargeItemAddDto;
import com.one.group.model.dto.SpChargeItemsQueryDto;
import com.one.group.model.dto.SpChargeItemsUpdateDto;
import com.one.group.model.entity.SpChargeItems;
import com.one.group.model.vo.SpChargeItemsVo;
import com.one.group.result.Result;
import com.one.group.service.SpChargeItemsService;
import com.one.group.utils.ResultUtil;
import com.one.group.utils.ThrowUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spChargeItem")
@Tag(name = "收费项目接口")
public class SpChargeItemsController {

    @Autowired
    private SpChargeItemsService spChargeItemsService;

    @GetMapping("/findByChargeTypeId")
    @Operation(summary = "根据收费类型id查询对应的收费项目")
    public Result<List<SpChargeItems>> find(@RequestParam Long id){
        //1.参数校验
        ThrowUtil.throwIf(id == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtil.success(spChargeItemsService.findByChargeTypeId(id));
    }
    @PostMapping("/findListByQuery")
    @Operation(summary = "查询收费项目列表")
    public Result findListByQuery(@RequestBody SpChargeItemsQueryDto spChargeItemsQueryDto){
        PageHelper.startPage(spChargeItemsQueryDto.getPage(),spChargeItemsQueryDto.getLimit());
        List<SpChargeItemsVo> spChargeItemsVo = spChargeItemsService.findListByQuery(spChargeItemsQueryDto);
        PageInfo<SpChargeItemsVo> pageInfo = new PageInfo<>(spChargeItemsVo);
        return ResultUtil.success(pageInfo);
    }
    @PostMapping("/add")
    @Operation(summary = "添加收费项目")
    public Result add(@RequestBody SpChargeItemAddDto spChargeItemsAddDto){
        if(spChargeItemsService.myAdd(spChargeItemsAddDto)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PutMapping("/update")
    @Operation(summary = "修改收费项目")
    public Result update(@RequestBody SpChargeItemsUpdateDto spChargeItemsUpdateDto){
        if(spChargeItemsService.myUpdate(spChargeItemsUpdateDto)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除收费项目")
    public Result delete(Long id){
        if(spChargeItemsService.myRemoveById(id)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
}
